package com.helloworld.chat.action;

import com.helloworld.chat.entity.User;
import com.helloworld.chat.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import static com.opensymphony.xwork2.Action.NONE;

/**
 * Created by xdzy on 2017/6/28.
 */
@Controller
@Scope("prototype")
public class UserAction implements ModelDriven<User>{
    private UserService userService;
    private User user=new User();
    private File logoUploadFile;
    private String logoUploadFileContentType;
    private String logoUploadFileFileName;
    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public String loginForm(){
        return "loginForm";
    }
    public String registerForm(){
        return "registerForm";
    }
    public String login(){
        User u=userService.login(user);
        if(u==null) return "lerror";
        ServletActionContext.getRequest().getSession()
                .setAttribute("loginUser",u);
        return "success";
    }
    public String logout(){
        ServletActionContext.getRequest().getSession().invalidate();
        return "logout";
    }
    @Override
    public User getModel() {
        return user;
    }
    public String checkUserName() throws IOException
    {
        //User user=employeeService.findByuserName(employee.getLoginName());
        User existuser=userService.findByUserName(user.getUsername());
        ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
        if(existuser==null)
        {
            ServletActionContext.getResponse().getWriter().println("1");
        }
        else
        {
            ServletActionContext.getResponse().getWriter().println("2");
        }

        return NONE;
    }

    public String repasswordCheck() throws IOException
    {

        String repassword= ServletActionContext.getRequest().getParameter("repassword");
        String password=ServletActionContext.getRequest().getParameter("password");
        if(!repassword.equals(password))
        {
            ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
            ServletActionContext.getResponse().getWriter().println("1");
        }
        else
        {
            ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
            ServletActionContext.getResponse().getWriter().println("2");
        }

        return NONE;
    }

    public String register()
    {
        userService.register(user);
        return "registerSuccess";
    }
    public String MyInfo()
    {
        HttpServletRequest request= ServletActionContext.getRequest();
        User u= (User) request.getSession().getAttribute("loginUser");

        User user=userService.findById( u.getUid());
        request.setAttribute("myinfo", user);
        return "MyInfo";
    }
    public String editUI()
    {

        HttpServletRequest request= ServletActionContext.getRequest();
        user= (User) request.getSession().getAttribute("loginUser");
        request.setAttribute("myinfo", user);

        return "editUI";
    }
    public String update() throws Exception
    {
        if(this.getLogoUploadFileFileName()!=null){
            String realPath=ServletActionContext.getServletContext().getRealPath("/resources/images");
            String fName=this.getLogoUploadFileFileName();
            String newName=user.getUsername()+fName.substring(fName.lastIndexOf('.'),fName.length());
            File file=new File(realPath+"/"+newName);
            if(file.exists()){
                file.delete();
            }
            FileOutputStream fos = new FileOutputStream(realPath+"/"+newName);
            FileInputStream fis = new FileInputStream(getLogoUploadFile());
            byte[] b = new byte[1024];
            int len=0;
            while((len=fis.read(b))>0)
            {
                fos.write(b, 0, len);
            }
            user.setHeadImg(newName);
        }else{
            User u= (User) ServletActionContext.getRequest().getSession().getAttribute("loginUser");
            user.setHeadImg(u.getHeadImg());
        }
        userService.update(user);
        ServletActionContext.getRequest().getSession().setAttribute("loginUser",user);
        return "updateSuccess";
    }

    public File getLogoUploadFile() {
        return logoUploadFile;
    }

    public void setLogoUploadFile(File logoUploadFile) {
        this.logoUploadFile = logoUploadFile;
    }

    public String getLogoUploadFileContentType() {
        return logoUploadFileContentType;
    }

    public void setLogoUploadFileContentType(String logoUploadFileContentType) {
        this.logoUploadFileContentType = logoUploadFileContentType;
    }

    public String getLogoUploadFileFileName() {
        return logoUploadFileFileName;
    }

    public void setLogoUploadFileFileName(String logoUploadFileFileName) {
        this.logoUploadFileFileName = logoUploadFileFileName;
    }
}
